package com.payment;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.slayer.model.PaypalBucket;
import com.slayer.model.impl.PaypalBucketImpl;
import com.slayer.service.PaypalBucketLocalServiceUtil;

public class PaymentBucketPortlet extends MVCPortlet {

	public void saveAccountDetails(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException, SystemException {
		
		long bucketId=0l;
		ArrayList<Long> bucketIdList = new ArrayList<Long>();
		ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
		List<PaypalBucket> paypalBucket = PaypalBucketLocalServiceUtil.getByCompanyId(themeDisplay.getCompanyId());
		
		int[] accountListIndexes = StringUtil.split(
				ParamUtil.getString(actionRequest, "bucketListIndexes"), 0);

		for (int i = 0; i < accountListIndexes.length; i++) {
			int accListIndex = accountListIndexes[i];
			
			bucketId = ParamUtil.getLong(actionRequest, "bucketId"+ accListIndex);
			
			String title = ParamUtil.getString(actionRequest, "title"
					+ accListIndex);
			String apiUserName = ParamUtil.getString(actionRequest,
					"apiUserName" + accListIndex);
			String apiPassword = ParamUtil.getString(actionRequest,
					"apiPassword" + accListIndex);
			String apiSignature = ParamUtil.getString(actionRequest,
					"apiSignature" + accListIndex);
			String description = ParamUtil.getString(actionRequest,
					"description" + accListIndex);
			int defaultEntry = ParamUtil.getInteger(actionRequest,
					"defaultEntry");
			
			if(!isValidEntry(title,apiUserName,apiPassword,apiSignature)){
				SessionErrors.add(actionRequest, "invalidData");
				break;
			}
			bucketId = updateItem(i,accListIndex, actionRequest,paypalBucket,themeDisplay,bucketId,title,apiUserName,apiPassword,apiSignature,description,defaultEntry);
			bucketIdList.add(bucketId);

			if (accountListIndexes.length == bucketIdList.size()) {
				removeItem(i,actionRequest,bucketIdList,paypalBucket);
			}
		}
	}
	
	private long updateItem(int i,int index, PortletRequest actionRequest,List<PaypalBucket> paypalBucket,ThemeDisplay themeDisplay,
			long bucketId,String title,String apiUserName,String apiPassword,String apiSignature,String description,int defaultEntry) {

		PaypalBucket bucket = null;
	
		long companyId = themeDisplay.getCompanyId();

			if (paypalBucket == null || paypalBucket.isEmpty()) {
				List<PaypalBucket> newList = new ArrayList<PaypalBucket>();
				newList.add(new PaypalBucketImpl());
				paypalBucket = newList;	
			}
			if (bucketId == 0l) {
				try {
					bucketId = CounterLocalServiceUtil.increment();
				} catch (SystemException e) {
					e.printStackTrace();
				}
				bucket = PaypalBucketLocalServiceUtil.createPaypalBucket(bucketId);
				bucket.setDateAdded(new Date());
			}else{
				try {
					bucket =PaypalBucketLocalServiceUtil.getPaypalBucket(bucketId);
					bucket.setDateModified(new Date());
				} catch (Exception e) {
					e.printStackTrace();
				} 
			}
			bucket.setBucketId(bucketId);
			bucket.setCompanyId(companyId);
			bucket.setTitle(title);
			bucket.setApiUserName(apiUserName);
			bucket.setApiPassword(apiPassword);
			bucket.setApiSignature(apiSignature);
			bucket.setDateModified(new Date());
			bucket.setDescription(description);
			bucket.setDefaultEntry(i == defaultEntry);
			try {
				PaypalBucketLocalServiceUtil.updatePaypalBucket(bucket);
			} catch (SystemException e) {
				e.printStackTrace();
			}
		return bucketId;
	}
	
	private void removeItem(int i,ActionRequest actionRequest,ArrayList<Long> bucketIdList,List<PaypalBucket> paypalBuckets) throws SystemException  {
	
		for (PaypalBucket bucket:paypalBuckets){
			if (paypalBuckets.size()!=1 && !bucketIdList.contains(bucket.getBucketId())) {
				PaypalBucketLocalServiceUtil.deletePaypalBucket(bucket);
			}
		}
	}
	private boolean isValidEntry(String title,String apiUserName,String apiPassword,String apiSignature){
		int passLength = apiPassword.length();
		if(title=="" || apiUserName == "" || apiPassword == "" || passLength<=6 || passLength>=20 || apiSignature== "")		
			return false;
		else 
			return true;
	}
}